<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<html>

<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta content="always" name="referrer">
<meta charset="utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<!-- 移动端必须的设置 -->
<meta name="viewport"
	content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<!-- 禁用电话号码检测功能，禁止对数字随便可以拨号 -->
<meta name="format-detection" content="telephone=no">
<!-- 禁用E-mail检测功能 -->
<meta name="format-detection" content="email=no">
<!-- 禁用地址检测功能，禁止跳转到地图 -->
<meta name="format-detection" content="address=no">
<!-- 在苹果设备上以全屏方式打开本网页 -->
<meta name="apple-mobile-web-app-capable" content="yes">
<!-- 设置在苹果设备上状态的颜色，默认白色，要设置成什么颜色以Web颜F色的风格而定 -->
<meta name="apple-mobile-web-app-status-bar-style" content="white">
<title>途狗收油</title>
<script type="text/javascript" src="../js/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="../js/base64.js"></script>
<script type="text/javascript" src="../js/layui/layui.js "></script>
<script type="text/javascript" src="../js/doT.min.js"></script>
<link rel="stylesheet" href="../js/layui/css/layui.css" />
<link rel="stylesheet"
	href="../vendor/font-awesome/css/font-awesome.min.css" />
<link rel="stylesheet" href="../css/basestyle.css" />
<!--  <script type="text/javascript" src="../vendor/resize.js"></script>-->
<!-- 业务js.css -->
<link rel="stylesheet" href="../busi/css/aui.css" />
<link rel="stylesheet" href="../busi/css/weui.min.css" />
<link rel="stylesheet" href="../busi/css/company_add.css" />
<link rel="stylesheet" href="../busi/css/aui-pull-refresh.css" />
<script type="text/javascript" src="../busi/js/jquery-3.2.1.min.js"></script>

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=MBRn8xBeYgyWguzmNo4jpOir497bPIGg"></script>
<script type="text/javascript" src="../busi/js/zepto.js"></script>
<script type="text/javascript" src="../busi/js/aui-pull-refresh.js"></script>
<script type="text/javascript" src="../busi/js/PullRefresh.js"></script>
<script type="text/javascript" src="../busi/js/bdMap.js"></script>
<script type="text/javascript" src="../js/popupstyle.js"></script>
<!-- 上传js -->

<title>创建产废单位</title>
</head>

<body>
	<%@ include file="../basejsp.jsp"%>
	<header class="aui-bar aui-bar-nav aui-bar-light">
			<a class="aui-pull-left aui-btn"  onclick="busiReback()">
				<span class="aui-iconfont aui-icon-left"></span>
			</a>
			<div class="aui-title">创建产废单位</div>
			<!--<a class="aui-pull-right aui-btn">
				<span class="aui-iconfont aui-icon-more"></span>
			</a> -->
		</header>
		<div class="companyAdd-form aui-refresh-content">
			<form>
				<ul class="aui-list aui-form-list">
				<!--  	<li class="aui-list-header" style="color: #FFFFFF;">创建中</li>-->
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div  class="aui-list-item-label">
								单位名称：
							</div>
							<div class="aui-list-item-input">
								<input id="orgname" type="text" placeholder="请填写单位名称...">
							</div>
						</div>
					</li>
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-label">
								联系人：
							</div>
							<div class="aui-list-item-input">
								<input id="realname" type="text" placeholder="请填写联系人名称...">
							</div>
						</div>
					</li>
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-label">
								联系电话：
							</div>
							<div class="aui-list-item-input">
								<input id="phone" type="text" placeholder="请填写联系电话...">
							</div>
						</div>
					</li>
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-label">
								联系地址：
							</div>
							<div class="aui-list-item-input">
								<input id="address" type="text" placeholder="请填写联系地址...">
							</div>
						</div>
					</li>
					<li class="aui-list-header" style="background-color: #FFFFFF;color: #333333;font-size: 0.8rem;">备注</li>
					<li class="aui-list-item">
						<div class="aui-list-item-inner">
							<div class="aui-list-item-input">
								<textarea id="remark" placeholder="请填写备注信息" style="border: 1px solid #d2d2d2;padding: 0.75rem;border-radius: 5px;    min-height: 5rem;"></textarea>
							</div>
						</div>
					</li>
					<li>
						<div class="weui-gallery" id="gallery">
							<span class="weui-gallery__img" id="galleryImg"></span>
							<div class="weui-gallery__opr">
								<a href="javascript:" class="weui-gallery__del">
									<i class="weui-icon-delete weui-icon_gallery-delete"></i>
								</a>
							</div>
						</div>
						<div class="weui-cell">
							<div class="weui-cell__bd">
								<div class="weui-uploader">
									<div class="weui-uploader__hd">
										<p class="weui-uploader__title">图片上传</p>
									</div>
									<div class="weui-uploader__bd">
										<ul class="weui-uploader__files" id="uploaderFiles">
										</ul>
										<div class="weui-uploader__input-box">
											<input id="uploaderInput" class="weui-uploader__input" type="file" accept="image/*" multiple="">
											<input id="fileUrl" type="hidden">
										</div>
									</div>
								</div>
							</div>
						</div>
					</li>
					<li class="aui-list-header" style="background-color: #FFFFFF;color: #333333;font-size: 0.8rem;">地址定位：</li>
					<li class="aui-content-padded">
						<div id="map2" style="height:50vh;width:100%;border:1px solid #bcbcbc;"></div>
						<div id="allmap"></div>
						<input id="nxp" type="hidden">
						<!--你的当前位置：
						<div id="mapPoint" ></div>
						你调整之后的位置：
						 <span id="show"></span>-->
					</li>
				</ul>
				<div class="companyAdd-form-button">
					<button type="button" onclick="busiAddSave()">确认登记</button>
				</div>		
			</form>
		</div>
		<div id="allmap"></div>
	<script>
	$(function() {
		if (getCookie("mobiletokenuserbusiuser")==null||getCookie("mobiletokenuserbusiuser")=='') {
			location.href="../busiuser/login.jsp";
		}
	});
	function busiReback(){
		location.href="../busiuser/busilist.jsp";
	}
	function busiAddSave(){
		var m = $("#phone").val();
		if(!m.match(/^1[34578]\d{9}$/)) {
			layer.msg('输入有效的手机号');
			/* tippopup("#mobile","输入有效的手机号")*/
		}else{
			var realname = $("#realname").val();
			var phone = $("#phone").val();
			var address = $("#address").val();
			var orgname = $("#orgname").val();
			var remark = $("#remark").val();
			var maploc = $("#nxp").val();
			var fileUrl = $("#fileUrl").val();
			var arr=maploc.split(",");
			var targetaddress; 
			$.ajax({
				type: "get",
				url: "http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + arr[1]+","+arr[0] + "&output=json&pois=1&ak=MBRn8xBeYgyWguzmNo4jpOir497bPIGg",
				dataType: 'jsonp',
				jsonp: "callback",
				jsonpCallback: "success_jsonpCallback",
				async: true,
				success: function(data) {
					targetaddress = data.result.formatted_address;
					var json = {
							"userid": "" + getCookie("useridbusiuser") + "",
							"realname": "" + realname + "",
							"phone": "" + phone + "",
							"address": "" + address + "",
							"orgname": "" + orgname + "",
							"remark": "" + remark + "",
							"longitude": "" + arr[0] + "",
							"latitude": "" + arr[1] + "",
							"targetaddress": "" + targetaddress + "",
							"fileurl": "" + fileUrl + "",
						}
						$.ajax({
							cache: false,
							type: "post",
							datatype: 'json',
							contentType: 'application/json;charset=UTF-8',
							url: ''+httpipstr+'alfa-mobile-ws/rest/registerbehavior/insertUser?mobiletoken='+getCookie("mobiletokenuserbusiuser")+'',
							data: JSON.stringify(json),
							success: function(data) {
								if (data.message=="1") {
									//触发事件
									layer.closeAll();
									location.href="../busiuser/busilist.jsp";
								}else if(data.message=="3"){
									layer.msg('数据添加失败');
								}else if(data.message=="2"){
									layer.msg('数据已存在');
								}else{
									layer.msg('系统错误');
								}
							},
							error: function (xhr, textStatus) {
				                if (xhr.status==0) {
				                	location.href="../busiuser/login.jsp";
								}
				            }
				  		});
					}
				});
			}	
		}
	</script>
	<script type="text/javascript">
	// 百度地图API功能
	var map = new BMap.Map("allmap");
	var point = new BMap.Point(116.331398, 39.897445);
	map.centerAndZoom(point, 12);

	var geolocation = new BMap.Geolocation();
	geolocation.getCurrentPosition(function(r) {
		if(this.getStatus() == BMAP_STATUS_SUCCESS) {
			var mk = new BMap.Marker(r.point);
			map.addOverlay(mk);
			map.panTo(r.point);
			//$('#mapPoint').html(r.point.lng + ',' + r.point.lat);
			$('#nxp').val(r.point.lng + ',' + r.point.lat);
			//	存取经纬坐标
			var point = r.point.lng + ',' + r.point.lat;
			var point01 = r.point.lat + ',' + r.point.lng;
			ShowMap("map2", {
				city: '重庆',
				addr: '',
				title: '',
				lawfirm: '',
				tel: '',
				mapx: point,
				pic: '',
				ismove: '1',
				piobj: 'nxp'
			});
			//	地址逆向解析获取位置信息
			$.ajax({
				type: "get",
				url: "http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=" + point01 + "&output=json&pois=1&ak=MBRn8xBeYgyWguzmNo4jpOir497bPIGg",
				dataType: 'jsonp',
				jsonp: "callback",
				jsonpCallback: "success_jsonpCallback",
				async: true,
				success: function(data) {
					new_content = data.result.formatted_address;
				}
			});
		} else {
		}
	}, {	
		enableHighAccuracy: true
	});
	function setAdd(v) {
		document.getElementById("show").innerHTML = v;
	}
	//getBDAddress(setAdd);

	//	下拉刷新
	pullRefresh();
	</script>
	<script type="text/javascript" class="uploader js_show">
		$(function() {
			var flag = 0;
			// 允许上传的图片类型
	        var allowTypes = ['image/jpg', 'image/jpeg', 'image/png', 'image/gif', 'image/bmp'];
	        // 4MB
	        var maxSize = 10 *1024 * 1024;
	        var maxWidth = 800;
			var tmpl = '<li class="weui-uploader__file" style="background-image:url(#url#)"></li>',
				$gallery = $("#gallery"),
				$galleryImg = $("#galleryImg"),
				$uploaderInput = $("#uploaderInput"),
				$uploaderFiles = $("#uploaderFiles");

			$uploaderInput.on("change", function(e) {
				
				var src, url = window.URL || window.webkitURL || window.mozURL,
					files = e.target.files;
				for(var i = 0, len = files.length; i < len; ++i) {
					var file = files[i];
					if (flag == 1) {
						layer.msg('请勿重复上传');
		                continue;
					}
					if (allowTypes.indexOf(file.type) === -1) {
						layer.msg('只能上传jpg,jpeg,png,gif,bmp类型的图片');
		                continue;
		            }
		            if (file.size > maxSize) {
		            	layer.msg('图片超过10M，不允许上传');
		                continue;
		            }
		            var base64 ;
		            var reader = new FileReader();
		            reader.readAsDataURL(file);
		            reader.onload = function (e) {
		            	var url = reader.result;
	                    var img = new Image();
	                    var canvas = document.createElement('canvas');
	                    var ctx = canvas.getContext('2d');
	                    img.onload = function () {
	                        // 不要超出最大宽度
	                        var w = Math.min(maxWidth, img.width);
	                        // 高度按比例计算
	                        var h = img.height * (w / img.width);
	                        // 设置 canvas 的宽度和高度
	                        canvas.width = w;
	                        canvas.height = h;
	                        ctx.drawImage(img, 0, 0, w, h);
	                        base64 = canvas.toDataURL('image/jpeg');
	                        $.ajax({
	                            type: "POST",
	                            url: ''+httpipstr+'alfa-mobile-ws/rest/file/uploadbase64?mobiletoken='+getCookie("mobiletokenuserbusiuser")+'',
	                            data: JSON.stringify({"files":base64}),
	                        	contentType: 'application/json;charset=UTF-8',
	                            dataType: 'json',
	                            cache: false,
	                            success: function (data) {
	                               $("#fileUrl").val(data.fileName);
	                               flag=1;
	                            },
	                            error: function (xhr, textStatus) {
	            	                if (xhr.status==0) {
	            	                	location.href="../busiuser/login.jsp";
	            					}
	            	            }
	                        });
	                    };
	                    img.src = url;
		            };
					
					if(url) {
						src = url.createObjectURL(file);
					} else {
						src = e.target.result;
					}

					$uploaderFiles.append($(tmpl.replace('#url#', src)));
				}
			});
			$uploaderFiles.on("click", "li", function() {
				$galleryImg.attr("style", this.getAttribute("style"));
				$gallery.fadeIn(100);
			});
			$gallery.on("click", function() {
				$gallery.fadeOut(100);
			});
		});
	</script>
</body>

</html>